package com.boss.salary.workflow.service.impl;

import com.boss.common.entity.WfActionDTO;
import com.boss.common.service.IWorkflowBusiness;
import com.boss.framework.exception.AppRuntimeException;
import com.boss.salary.dao.IAgencyVerifyDao;
import com.boss.salary.entity.AgencyVerify;
import com.boss.salary.workflow.service.ISalaryWorkFlowService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SalaryWorkFlowServiceImpl implements ISalaryWorkFlowService {

    /**
     * 工资流程定义key
     */
    private static final String SALARY_PROCESS_DEF_KEY = "salary_agency_verify";

    /**
     * 工作流实现类
     */
    @Autowired
    private IWorkflowBusiness workflowBusiness;

    @Autowired
    private IAgencyVerifyDao iAgencyVerifyDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doSalaryFlowList(UserDTO userDTO, WfActionDTO wfActionDTO) throws AppRuntimeException {
        wfActionDTO.setProcessDefKey(SALARY_PROCESS_DEF_KEY);
        wfActionDTO.setUserId(userDTO.getGuid());
        wfActionDTO.setYear(String.valueOf(userDTO.getYear()));
        wfActionDTO.setProvince(userDTO.getProvince());
        workflowBusiness.doWorkFlowList(userDTO, wfActionDTO);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doSalaryFlow(UserDTO userDTO, WfActionDTO wfActionDTO) throws AppRuntimeException {
        wfActionDTO.setProcessDefKey(SALARY_PROCESS_DEF_KEY);
        wfActionDTO.setUserId(userDTO.getGuid());
        wfActionDTO.setYear(String.valueOf(userDTO.getYear()));
        wfActionDTO.setProvince(userDTO.getProvince());
        workflowBusiness.doWorkFlow(userDTO, wfActionDTO);
    }

    @Override
    public List<String> queryWorkflowInfo(UserDTO userDTO, WfActionDTO wfActionDTO) throws AppRuntimeException {
        wfActionDTO.setProcessDefKey(SALARY_PROCESS_DEF_KEY);
        List<String> list = workflowBusiness.queryWorkflowInfo(userDTO, wfActionDTO);
        return list;
    }

    @Override
    public List<Map> queryWorkflowInfoStatus(UserDTO userDTO, WfActionDTO wfActionDTO,List<String> statuses,List<String> bizkeyList) throws AppRuntimeException {
        wfActionDTO.setProcessDefKey(SALARY_PROCESS_DEF_KEY);
        List<Map> list = workflowBusiness.queryWorkFlowStatus(userDTO, wfActionDTO,statuses,bizkeyList);
        if(list.size()<0){
            return null;
        }else{
            return list;
        }
    }

    @Override
    public List<Map> getAuditInstruction(UserDTO userDTO, WfActionDTO wfActionDTO,String agencyId) {
        List<Map> list =new ArrayList<>();
        AgencyVerify agencyVerify = iAgencyVerifyDao.getInfoByAgencyId(agencyId);
        String bizKey=agencyVerify.getBizKey();
        wfActionDTO.setProcessDefKey(SALARY_PROCESS_DEF_KEY);
        wfActionDTO.setBusinessKeys(bizKey);
        wfActionDTO.setYear(String.valueOf(userDTO.getYear()));
        wfActionDTO.setProvince(userDTO.getProvince());
        List<Map> maps= workflowBusiness.getAuditInstruction(wfActionDTO);
        if(maps!=null&&maps.size()>0){
            String content="";
            String timestamp="";
            String color="";
            for (Map map : maps) {
                Map newMap =new HashMap();
                if("null".equals(String.valueOf(map.get("actionUser")))){
                    content="当前节点："+String.valueOf(map.get("nodeName"))+"；"+"操作人： ；"
                            + "状态：" +String.valueOf(map.get("actionName"))+"；"+ "意见："+ String.valueOf(map.get("message"));
                    timestamp=String.valueOf(map.get("actionTime"));
                    if("退回".equals(String.valueOf(map.get("actionName")))){
                        //红色
                        color="#FF0000";
                    }else{
                        //绿色
                        color="#00FF00";
                    }
                }else{
                    content="节点："+String.valueOf(map.get("nodeName"))+"；"+"操作人："+String.valueOf(map.get("actionUser"))+"；"
                            + "状态：" +String.valueOf(map.get("actionName"))+"；"+ "意见："+ String.valueOf(map.get("message"));
                    timestamp=String.valueOf(map.get("actionTime"));
                    if("退回".equals(String.valueOf(map.get("actionName")))){
                        //红色
                        color="#FF0000";
                    }else{
                        //绿色
                        color="#00FF00";
                    }
                }

                newMap.put("content",content);
                newMap.put("timestamp",timestamp);
                newMap.put("color",color);
                list.add(newMap);
            }
        }
        return list;
    }
}
